package com.liuboya.service.task.wbuser.Impl;

import com.alibaba.fastjson.JSONObject;
import com.liuboya.dao.weibo.wbuser.entity.WbUser;
import com.liuboya.service.tablebiz.wbuser.WbUserService;
import com.liuboya.service.task.wbuser.WbUserTask;
import com.liuboya.service.weiboapi.WbApiService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 描述：
 *
 * @author yeyuan
 * @date 2021/08/30 13:54
 **/
@EnableScheduling
@Service
@Slf4j
public class WbUserTaskImpl implements WbUserTask {

    @Resource
    private WbApiService wbApiService;

    @Resource
    private WbUserService wbUserService;

    @Value("${dev.flag}")
    protected Boolean devFlag;

    @Scheduled(fixedDelay = 1000 * 10)
    @Override
    public void feedAndDetailSaveWbUser() {
        if (devFlag) {
//            return;
        }
        log.info("微博feed流,开始爬取");
        JSONObject feed = wbApiService.feed();
        List<WbUser> wbUsers = wbUserService.feedJsonToUser(feed);
        for (WbUser wbUser : wbUsers) {
            Long weiboId = wbUser.getWeiboId();
            if (Objects.isNull(weiboId)) {
                continue;
            }
            WbUser byWeiBoId = wbUserService.findByWeiBoId(weiboId);
            if (Objects.nonNull(byWeiBoId)) {
                continue;
            }
            log.info("微博feed流,插入用户{}", wbUser.getName());
            wbUserService.insert(wbUser);
        }
        log.info("微博feed流,结束");
    }
}